﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace XMLGener.Utils
{
    /// <summary>
    /// Class used as util for file location
    /// </summary>
    public class DirectoriesUtil
    {
        /// <summary>
        /// Base directory of templates
        /// </summary>
        public static DirectoryInfo templatesDir = new DirectoryInfo("templates");

        /// <summary>
        /// Base directory of modules libraries
        /// </summary>
        public static DirectoryInfo modulesDir = new DirectoryInfo("modules");
        
        /// <summary>
        /// Working directory
        /// </summary>
        public static DirectoryInfo currentDir = new DirectoryInfo(".");
        
        /// <summary>
        /// output directory
        /// </summary>
        public static DirectoryInfo outputDir = new DirectoryInfo("out");

        /// <summary>
        /// Initialize and set working and other directories. 
        /// If working,modules,templates or out directory does not exist, 
        /// then this directory will be created.
        /// </summary>
        /// <param name="path">directory path</param>
        public static void setWorkingDirectory(string path)
        {
            currentDir = new DirectoryInfo(path);
            templatesDir = new DirectoryInfo(path + Path.DirectorySeparatorChar + "templates");
            modulesDir =  new DirectoryInfo(path + Path.DirectorySeparatorChar + "modules");
            outputDir = new DirectoryInfo(path + Path.DirectorySeparatorChar + "out");
            if (!currentDir.Exists) currentDir.Create();
            if (!templatesDir.Exists) templatesDir.Create();
            if (!modulesDir.Exists) modulesDir.Create();
            if (!outputDir.Exists) outputDir.Create();

        }
        
        /// <summary>
        /// Try to get relative path of file "path" referenced to directory refDirectory
        /// </summary>
        /// <param name="refDirectory">absolute path of reference directory</param>
        /// <param name="path">file absolute path</param>
        /// <returns>relative path according to refDirectory</returns>
        public static string getRelativePath(DirectoryInfo refDirectory, string path)
        {
            int i = 0;
            string result = path;
            StringBuilder sb = new StringBuilder();
            DirectoryInfo dir = refDirectory;
            // if directory is root directory or dehtf difference is more than 5, stop searching 
            // and return absolute path
            while (dir != null && i < 6)
            {
                if (path.StartsWith(dir.FullName))
                {
                    sb.Append( result.Replace(dir.FullName + Path.DirectorySeparatorChar, ""));
                    if (i > 0)
                        sb.Insert(0, ".." + Path.DirectorySeparatorChar, i);
                    result = sb.ToString();
                    return result;

                }
                i++;
                dir = dir.Parent;
            }

            return result ;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="directoryPath"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static string combinePath(string directoryPath, string filename)
        {
            FileInfo fi = new FileInfo(Path.Combine(directoryPath, filename));
            return  fi.FullName;
        }

    }
}
